import 'dart:io';
import 'package:flutter/services.dart';
import 'package:flutter_architecture/constant/env_enum.dart';
import 'package:flutter/material.dart';
import 'package:flutter_architecture/utils/app_update_util.dart';
import 'package:flutter_architecture/utils/log_util.dart';
import 'package:flutter_architecture/widgets/error/custom_error_widget.dart';
import 'package:package_info/package_info.dart';

/// 整个App的配置文件
/// 不同环境请求服务器地址配置
/// @author: dingwen
/// @date: 2020/12/28

class AppConfig {
  static Env env;

  ///初始化路由Key
  ///通过GlobalKey的方式防止出现context的问题
  static GlobalKey<NavigatorState> globalRouteKey = GlobalKey<NavigatorState>();

  /// 获取主服务器地址
  static String get mainServerUrl {
    switch (env) {
      case Env.DEV:
        return 'http://192.168.0.253';
      case Env.LOCAL:
        return 'http://192.168.0.117:8080';
      case Env.PROD:
        return 'http://192.168.0.117:8080';
      case Env.NETWORK:
        return 'http://192.168.0.117:8080';
      default:
        return 'http://192.168.0.117:8080';
    }
  }

  /// 获取WebView服务器地址
  static String get webViewServerUrl {
    switch (env) {
      case Env.DEV:
        return 'http://192.168.0.117:8080';
      case Env.LOCAL:
        return 'http://192.168.0.117:8080';
      case Env.PROD:
        return 'http://192.168.0.117:8080';
      case Env.NETWORK:
        return 'http://192.168.0.117:8080';
      default:
        return 'http://192.168.0.117:8080';
    }
  }

  /// 获取App更新地址
  static String get appUpdateServerUrl {
    switch (env) {
      case Env.DEV:
        // return 'http://192.168.0.83:8081/app_update_model.json';
        return 'http://192.168.0.83:8081/update_test.json';
      case Env.LOCAL:
        return 'http://192.168.0.117:8080';
      case Env.PROD:
        return 'http://192.168.0.117:8080';
      case Env.NETWORK:
        return 'http://192.168.0.117:8080';
      default:
        return 'http://192.168.0.117:8080';
    }
  }

  /// 白名单
  static List<String> get whiteList {
    if (env == Env.NETWORK) {
      return [
        "/action?uri=%2Foauth-service%2Fcode%3Ftype%3Dpicture",
        "/action?uri=%2Foauth-service%2Fdevice%2Fbinding"
      ];
    } else {
      return [
        "/oauth-service/code?type=picture",
        "/oauth-service/device/binding"
      ];
    }
  }

  ///登录路径
  static List<String> get authList {
    if (env == Env.NETWORK) {
      return [
        "/action?uri=%2Foauth-service%2Flogin",
        "/action?uri=%2Foauth-service%2Fuser%2Freset-pwd-login"
      ];
    } else {
      return ["/oauth-service/login", "/oauth-service/user/reset-pwd-login"];
    }
  }

  ///token
  static Map<String, dynamic> get mapTokenParams {
    return {
      "url": 'http://yngcgis.yndlr.gov.cn/portal/sharing/rest/generateToken',
      "f": "json",
      "username": "forRegister",
      "password": "yngc2020@huitian",
      "referer": "yngcgis.yndlr.gov.cn",
      "expiration": "600"
    };
  }

  ///初始化错误错误页面
  ///[ErrorWidget]Flutter提供
  static void initCustomErrorPage() {
    ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails) {
      LogUtil.getInstance().e(LogUtil.formatStr(
          fileName: 'AppConfig',
          functionName: 'initCustomErrorPage',
          message: flutterErrorDetails.toString()));
      if (AppConfig.env == Env.NETWORK || AppConfig.env == Env.PROD) {
        return CustomErrorWidget();
      }
      return ErrorWidget(flutterErrorDetails);
    };
  }

  ///初始化状态栏
  static void initSystemUIOverlayStyle() {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarIconBrightness: Brightness.dark,
      statusBarBrightness:
          Platform.isAndroid ? Brightness.dark : Brightness.light,
      systemNavigationBarColor: Colors.white,
      systemNavigationBarDividerColor: Colors.grey,
      systemNavigationBarIconBrightness: Brightness.dark,
    ));
  }

  ///更新初始化
  static void initUpdate() {
    initXUpdate();
  }

  /// 检查更新
  static void checkAppUpdate() async {
    String currentAppVersionCode = await getCurrentAppVersionCode();
    List<String> currentAppVersionCodeList = currentAppVersionCode.split('.');
    //todo 获取服务器版本信息 1.0.0
    String serverAppVersionUpdate = '1.0.0';
    List<String> serverAppVersionCodeList = serverAppVersionUpdate.split('.');
    bool isUpdate = false;
    currentAppVersionCodeList.forEach((c) {
      serverAppVersionCodeList.forEach((s) {

      });
    });
    checkUpdateDefault();
  }

  /// 获取当前Apk包的版本信息[versionCode]
  static Future<String> getCurrentAppVersionCode() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    return packageInfo.version;
  }
}
